<?php
/**
 * Created by PhpStorm.
 * User: Xzareth
 * Date: 20/05/14
 * Time: 9:33 PM
 */
require 'init.php';
$secure->adminOnly();

if (isset($_POST['submit'])) {

    $inventoryID = $_REQUEST['inventoryID'];
    $colour = $_REQUEST['colour'];
    $size = $_REQUEST['size'];
    $unitsInStock = $_REQUEST['unitsInStock'];
    $productID = $_REQUEST['productID'];
    $productName = $_REQUEST['productName'];

    $modifyFormStatus = array();

    if ($_REQUEST['submit'] == "X") {
        $modifyFormStatus[] = $inventory->deleteInventory($inventoryID);

    } else if ($_REQUEST['submit'] == "Insert") {

        $modifyFormStatus[] = $inventory->addInventory($productID, $colour, $size, $unitsInStock);

    } else if ($_REQUEST['submit'] == "Update") {

        $updated = $product->updateInventory($inventoryID, $colour, $size, $unitsInStock);

        $modifyFormStatus[] = ($updated) ? "Inventory for: $productName was updated successfully!" : "Inventory for: $productName was NOT updated successfully!";
    }
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link href="css/main.css" rel="stylesheet" type="text/css">
    <script src="javascripts/validateForm.js"></script>
    <title>Stock</title>
</head>
<body>
<div id="container">
    <div id="leftCogSplash"><img src="images/cogLeft.png"/></div>
    <div id="rightCogSplash"><img src="images/cogRight.png"/></div>
    <?php include "navbar.php"; ?>
    <div id="content">
        <div id="adminDashboard">
            <div id="returnToDashboard">
                <a href="admin.php"><h3>Return to Dashboard</h3></a>
            </div>
            <?php include "stockDashboard.php"; ?>
            <div id="currentStock">
                <h3>Stock Levels</h3>
            </div>
            <div id="stockResults">
                <?php
                $rowNumber = 0;

                if (isset($_GET['stockResults'])) {
                    echo "<script type='text/javascript'>
                            document.getElementById('stockResults').style.display='block';
                            document.getElementById('modifyFormContainer').style.display='none';
                            </script>";
                }

                if (isset($_GET['quantity'])) {
                    switch ($_GET['quantity']) {
                        case "none":
                            $sql = "SELECT I.InventoryID, P.ProductName, I.Colour, I.Size, I.UnitsInStock FROM Inventory AS I LEFT JOIN Products AS P WHERE I.ProductID = P.ProductID AND UnitsInStock = 0";
                            break;
                        case "low":
                            $sql = "SELECT I.InventoryID, P.ProductName, I.Colour, I.Size, I.UnitsInStock FROM Inventory AS I LEFT JOIN Products AS P WHERE I.ProductID = P.ProductID AND UnitsInStock <= 20 AND UnitsInStock > 0";
                            break;
                        case "high":
                            $sql = "SELECT I.InventoryID, P.ProductName, I.Colour, I.Size, I.UnitsInStock FROM Inventory AS I LEFT JOIN Products AS P WHERE I.ProductID = P.ProductID AND UnitsInStock >= 20";
                            break;
                    }
                } else if (isset($_GET['stockID'])) {
                    $sql = "SELECT I.InventoryID, P.ProductName, I.Colour, I.Size, I.UnitsInStock FROM Inventory AS I LEFT JOIN Products AS P WHERE I.ProductID = P.ProductID AND InventoryID = '$_GET[stockID]'";
                } else {
                    $sql = "SELECT I.InventoryID, P.ProductName, I.Colour, I.Size, I.UnitsInStock FROM Inventory AS I LEFT JOIN Products AS P WHERE I.ProductID = P.ProductID";
                }

                foreach ($db->query($sql) as $row) {
                    $rowNumber++;
                    ?>
                    <table>
                        <thead <?php if ($rowNumber !== 1) echo "style='display: none'" ?>>
                        <tr>
                            <th class="inventoryIDCol">ID</th>
                            <th class="productNameCol">Product Name</th>
                            <th class="colourCol">Colour</th>
                            <th class="sizeCol">Size</th>
                            <th class="unitsInStockCol">Units</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td class="inventoryIDCol"><?php echo $row['InventoryID']; ?></td>
                            <td class="productNameCol"><?php echo $row['ProductName']; ?></td>
                            <td class="colourCol"><?php echo $row['Colour']; ?></td>
                            <td class="sizeCol"><?php echo $row['Size']; ?></td>
                            <td class="unitsInStockCol"><?php echo $row['UnitsInStock']; ?></td>
                        </tr>
                        </tbody>
                    </table>
                <?php
                }
                ?>
            </div>
            <div id="modifyFormContainer">
                <?php
                if (isset($_GET['modifyStock'])) {
                    echo "<script type='text/javascript'>
                            document.getElementById('stockResults').style.display='none';
                            document.getElementById('modifyFormContainer').style.display='block';
                            </script>";
                }
                $rowNumber = 0;
                $sql = "SELECT I.InventoryID, P.ProductID, P.ProductName, I.Colour, I.Size, I.UnitsInStock FROM Inventory AS I LEFT JOIN Products AS P WHERE I.ProductID = P.ProductID";
                foreach ($db->query($sql) as $row) {
                    $rowNumber++;
                    ?>
                    <form id="modifyForm" name="modifyForm" method="post" action="">
                        <table>
                            <thead <?php if ($rowNumber !== 1) echo "style='display: none'" ?>>
                            <tr>
                                <th class="inventoryIDCol"><label>ID</label></th>
                                <th class="productNameCol"><label>Product Name</label></th>
                                <th class="colourCol"><label for='colour'>Colour</label></th>
                                <th class="sizeCol center"><label for='size'>Size</label></th>
                                <th class="unitsInStockCol center"><label for='unitsInStock'>Units</label></th>
                                <th></th>
                                <th></th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                                <td class="inventoryIDCol"><?php echo $row['InventoryID']; ?></td>
                                <td class="productNameCol"><?php echo $row['ProductName']; ?></td>
                                <td class="colourCol"><input type='text' name='colour' id='colour'
                                                             value='<?php echo $row['Colour']; ?>'/></td>
                                <td class="sizeCol center"><input type='text' name='size' id='size'
                                                                  value='<?php echo $row['Size']; ?>'/></td>
                                <td class="unitsInStockCol"><input type='text' name='unitsInStock' id='unitsInStock'
                                                                   value='<?php echo $row['UnitsInStock']; ?>'/></td>
                                <td class="updateBtn"><input type="submit" name="submit" value="Update"/></td>
                                <td class="deleteBtn"><input type='submit' name='submit' value='X' class='red'
                                                             id="deleteButton"></td>
                            </tr>
                            </tbody>
                        </table>
                        <input type='hidden' name='inventoryID' id='inventoryID'
                               value='<?php echo $row['InventoryID']; ?>'/>
                        <input type='hidden' name='productID' id='productID' value='<?php echo $row['ProductID']; ?>'/>
                        <input type='hidden' name='productName' id='productName'
                               value='<?php echo $row['ProductName']; ?>'/>
                    </form>
                <?php
                }
                ?>
            </div>
            <div id="modifyFormStatusBar">
                <?php
                if (!empty($modifyFormStatus)) {
                    echo "<script type='text/javascript'>document.getElementById('modifyFormStatusBar').style.display = 'block';</script>";
                    echo '<p>' . implode('<p></p>', $modifyFormStatus) . '</p>';
                    echo "<script type='text/javascript'>clearMessage('modifyFormStatusBar', 2000)</script>";
                }
                // close the database connection
                $db = null;
                ?>
            </div>
        </div>
        <?php require 'footer.php' ?>
    </div>

</body>
</html>